﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BE.Abastos;

namespace SAIN.Wuc
{

    public partial class wucCategoriiesTree : System.Web.UI.UserControl
    {
        private List<Category> lista = new List<Category>();
        public void Actualizar()
        {
            lista = new BC.Abastos.CategoriasBC().Select(
                new Category()
                {
                    iProductCategoryId = -1,
                    iProductCategoryParentId = -1,
                    sCategoryName = string.Empty,
                    sDescription = string.Empty
                });

            if (!this.IsPostBack)
            {
                List<Category> Parents = new List<Category>();

                Parents = (from o in lista
                           where o.iProductCategoryParentId == null || o.iProductCategoryParentId == -1
                           select o).ToList();

                this.Tree.Nodes.Clear();
                foreach (Category item in Parents)
                {
                    this.Tree.Nodes.Add(new TreeNode(item.sCategoryName, item.iProductCategoryId.ToString()));
                }
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            lista = new BC.Abastos.CategoriasBC().Select(
                new Category() 
                { 
                    iProductCategoryId=-1,
                    iProductCategoryParentId=-1,
                    sCategoryName=string.Empty,
                    sDescription=string.Empty
                });

            if (!this.IsPostBack)
            {
                List<Category> Parents = new List<Category>();

                Parents = (from o in lista
                           where o.iProductCategoryParentId == null || o.iProductCategoryParentId == -1
                          select o).ToList();

                this.Tree.Nodes.Clear();
                foreach (Category item in Parents)
                {
                    this.Tree.Nodes.Add(new TreeNode(item.sCategoryName, item.iProductCategoryId.ToString()));
                }
            }
        }

        protected void Tree_TreeNodeExpanded(object sender, TreeNodeEventArgs e)
        {
            List<Category> hojas = new List<Category>();

            hojas = (from o in lista
                     where o.iProductCategoryParentId == int.Parse(e.Node.Value)
                     select o).ToList();
            foreach( Category item in hojas)
            {
                e.Node.ChildNodes.Add(new TreeNode(item.sCategoryName, item.iProductCategoryId.ToString()));
            }
        }

        protected void Tree_SelectedNodeChanged(object sender, EventArgs e)
        {
            List<Category> hojas = new List<Category>();

            hojas = (from o in lista
                     where o.iProductCategoryParentId == int.Parse(this.Tree.SelectedNode.Value)
                     select o).ToList();

            foreach (Category item in hojas)
            {
                bool flag = false;
                foreach (TreeNode hijo in this.Tree.SelectedNode.ChildNodes)
                {
                    flag = hijo.Text == item.sCategoryName ? true : flag;
                }
                if (flag == false)
                {
                    this.Tree.SelectedNode.ChildNodes.Add(new TreeNode(item.sCategoryName, item.iProductCategoryId.ToString()));
                }
            }
        }
    }
}